package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;

import com.example.dto.WagesDto;
import com.example.entity.User;
import com.example.entity.UserWages;
import com.example.entity.Position;
import com.example.entity.Wages;

import com.example.service.UserService;
import com.example.service.UserWagesService;
import com.example.service.PositionService;
import com.example.service.WagesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Api(tags = {"薪资接口列表"})
@RestController
@RequestMapping("/wages")
@Slf4j
public class WagesController {

    @Autowired
    private WagesService wagesService;

    @Autowired
    private PositionService positionService;

    @Autowired
    private UserWagesService userWagesService;

    @Autowired
    private UserService userService;


    /**
     * 工资界面分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @ApiOperation("查询薪资")
    @GetMapping("/list")
    public Result<?> list(int page, int pageSize, String name) {
        Page<Wages> wagesPage = new Page<>(page, pageSize);
        Page<WagesDto> wagesDtoPage = new Page<>(page, pageSize);

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), User::getUsername, name);
        List<User> userList = userService.list(wrapper);

        List<Long> ids = userList.stream().map(item -> {
            Long id = item.getId();
            return id;
        }).collect(Collectors.toList());

        LambdaQueryWrapper<Wages> wagesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wagesLambdaQueryWrapper.in(Wages::getUserId, ids);
        wagesService.page(wagesPage, wagesLambdaQueryWrapper);

        List<Wages> wagesPageRecords = wagesPage.getRecords();

        log.info("{}", wagesPageRecords);

        List<WagesDto> collect = wagesPageRecords.stream().map(item -> {

            WagesDto wagesDto = new WagesDto();
            BeanUtils.copyProperties(item, wagesDto);

            LambdaQueryWrapper<UserWages> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(UserWages::getWagesId, item.getId());
            UserWages one = userWagesService.getOne(wrapper1);

            wagesDto.setPositionId(one.getPositionId());
            wagesDto.setUserId(one.getUserId());

            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getId, one.getUserId());
            LambdaQueryWrapper<Position> positionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            positionLambdaQueryWrapper.eq(Position::getId, one.getPositionId());

            wagesDto.setUserName(userService.getOne(userLambdaQueryWrapper).getUsername());
            wagesDto.setPositionWages(positionService.getOne(positionLambdaQueryWrapper).getWages());


            Integer money = Integer.valueOf(wagesDto.getPositionWages()) + Integer.valueOf(wagesDto.getPerformance()) + Integer.valueOf(wagesDto.getSubsidy());

            wagesDto.setSalary(String.valueOf(money));

            wagesService.updateById(wagesDto);

            return wagesDto;
        }).collect(Collectors.toList());

        wagesDtoPage.setRecords(collect);
        Map<String, Object> map = new HashMap<>();
        map.put("total", wagesPage.getTotal());
        map.put("rows", wagesDtoPage.getRecords());
        return Result.success(map);

    }

    /**
     * 新增薪资信息
     *
     * @param wagesDto
     * @return
     */

    @ApiOperation("新增薪资")
    @PostMapping
    public Result<?> addWage(@RequestBody WagesDto wagesDto) {
        log.info("{}", wagesDto);
        long orderId = IdWorker.getId();
        wagesDto.setStatus("1");
        wagesDto.setId(orderId);
        wagesService.saveWithUW(wagesDto);
        return Result.success("新增薪资信息成功");
    }

    /**
     * 修改
     *
     * @param wagesDto
     * @return
     */
    @ApiOperation("修改薪资")
    @PutMapping
    public Result<?> updateWages(@RequestBody WagesDto wagesDto) {
        wagesService.updateByIdWithUW(wagesDto);
        return Result.success("修改薪资信息成功");
    }


    @ApiOperation("薪资申请通过判断")
    @PutMapping("/status")
    public Result<?> updateWages(@RequestBody Wages wages) {
        log.info("{}", wages);
        LambdaQueryWrapper<Wages> wagesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wagesLambdaQueryWrapper.eq(Wages::getId, wages.getId());
        Wages one = wagesService.getOne(wagesLambdaQueryWrapper);
        one.setStatus(wages.getStatus());

        wagesService.updateById(one);
        return Result.success("修改薪资信息成功");
    }

    /**
     * 返回修改界面
     *
     * @param id
     * @return
     */
    @ApiOperation("返回到修改界面的查询薪资")
    @GetMapping("/{id}")
    public Result<WagesDto> getById(@PathVariable Long id) {
        return Result.success(wagesService.getByIdWithUW(id));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除薪资")
    @DeleteMapping("/{id}")
    public Result<?> deleteWages(@PathVariable Long id) {
        wagesService.removeByIdWithUW(id);
        return Result.success("删除薪资信息成功");

    }
}
